package br.com.hsp.system.beans;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.com.hsp.organizacao.Consulta;
import br.com.hsp.organizacao.Prontuario;
import br.com.hsp.system.repository.ConsultaRepository;
import br.com.hsp.system.repository.ProntuarioRepository;

@ManagedBean
@ViewScoped
public class ProntuarioBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Prontuario prontuario = new Prontuario();
	
	@ManagedProperty(value="#{userSessionBean}")
	private UserSessionBean session;
	
	private Long medId;
	
	private Long pacienteId;

	private List<Prontuario> prontuarios;
	
	private List<Consulta> consultas;
	

	public void add() {
		EntityManager manager = this.getManager();
		ProntuarioRepository prontuarioRepository = new ProntuarioRepository(manager);

			if (this.prontuario.getId() == null) {
				prontuarioRepository.add(this.prontuario);
			} else {
				prontuarioRepository.atualiza(this.prontuario);
			}
		this.prontuario = new Prontuario();
	}

	public void preparaAlteracao() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		Long id = Long.parseLong(params.get("id"));
		EntityManager manager = this.getManager();
		ProntuarioRepository repository = new ProntuarioRepository(manager);
		this.prontuario = repository.procura(id);
	}

	public void preparaRemove() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		prontuario.setId(Long.parseLong(params.get("id")));
	}

	public void limpaRemove() {
		this.prontuario = new Prontuario();
	}

	public void remove() {
		EntityManager manager = this.getManager();
		ProntuarioRepository repository = new ProntuarioRepository(manager);
		repository.remove(prontuario.getId());
		this.prontuario = null;
		this.prontuarios = null;
	}

	private EntityManager getManager() {
		FacesContext fc = FacesContext.getCurrentInstance();
		ExternalContext ec = fc.getExternalContext();
		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
		return (EntityManager) request.getAttribute("EntityManager");

	}

	public List<Prontuario> getProntuarios() {
		if (this.prontuarios == null) {
			EntityManager manager = this.getManager();
			ProntuarioRepository repository = new ProntuarioRepository(manager);
			this.prontuarios = repository.getLista();

		}
		return this.prontuarios;
	}
	
	public List<Consulta> getConsultas() {
		if (this.consultas == null) {
			if (pacienteId == null) {
				setPacienteId(session.getTempId());
				session.setTempId(null);
			}
			EntityManager manager = this.getManager();
			ConsultaRepository repository = new ConsultaRepository(manager);
			this.setConsultas(repository.getConsultasPaciente(pacienteId));
			setPacienteId(null);
		}
		return this.consultas;
	}

	public void setConsultas(List<Consulta> consultas) {
		this.consultas = consultas;
	}

	public Prontuario getProntuario() {
		return prontuario;
	}

	public void setProntuario(Prontuario prontuario) {
		this.prontuario = prontuario;
	}

	public Long getMedId() {
		return medId;
	}

	public void setMedId(Long medId) {
		this.medId = medId;
	}

	public UserSessionBean getSession() {
		return session;
	}

	public void setSession(UserSessionBean session) {
		this.session = session;
	}

	public Long getPacienteId() {
		return pacienteId;
	}

	public void setPacienteId(Long pacienteId) {
		this.pacienteId = pacienteId;
	}
	
}
